package cError

import "fmt"

type ErrorInterface interface {
	Error() string
	Code() int32
}

type BaseError struct {
	ErrCode int32
	ErrMsg  string
}

func (i *BaseError) Error() string {
	return i.ErrMsg
}

func (i *BaseError) Code() int32 {
	return i.ErrCode
}

type CError string

const (
	CError_Csingo_Nil           CError = "success"
	CError_Csingo_System        CError = "系统错误"
	CError_Csingo_Conflict      CError = "错误值冲突"
	CError_Csingo_Unknown       CError = "未知错误"
	CError_Csingo_Param         CError = "参数错误"
	CError_Csingo_RouteNotFound CError = "路由不存在"
)

var (
	cError_error = map[CError]int32{
		CError_Csingo_Nil:           0,
		CError_Csingo_System:        1,
		CError_Csingo_Conflict:      2,
		CError_Csingo_Unknown:       3,
		CError_Csingo_Param:         4,
		CError_Csingo_RouteNotFound: 5,
	}
)

func (x CError) Error() string {
	return string(x)
}

func (x CError) Code() int32 {
	return cError_error[x]
}

func (x CError) Merge(err error) *BaseError {
	return &BaseError{
		ErrCode: x.Code(),
		ErrMsg:  fmt.Sprintf("%s: %s", x, err),
	}
}
